package com.zeny.coding.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zeny.coding.constants.CourseConstants;
import com.zeny.coding.dto.PageDto;
import com.zeny.coding.dto.ResourceDto;
import com.zeny.coding.entity.Resource;
import com.zeny.coding.entity.ResourceExample;
import com.zeny.coding.entity.RoleResource;
import com.zeny.coding.entity.RoleResourceExample;
import com.zeny.coding.mapper.ResourceMapper;
import com.zeny.coding.mapper.RoleResourceMapper;
import com.zeny.coding.service.ResourceService;
import com.zeny.coding.util.SnowFlake;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <h2>资源管理服务</h2>
 *
 * @Author Zeny
 */
@SuppressWarnings("all")
@Service
public class ResourceServiceImpl implements ResourceService {

    @Autowired
    private ResourceMapper resourceMapper;

    @Autowired
    private RoleResourceMapper roleResourceMapper;

    @Override
    public void list(PageDto<ResourceDto> pageDto) {
        //分页
        PageHelper.startPage(pageDto.getPage(), pageDto.getSize());
        //设置条件
        ResourceExample resourceExample = new ResourceExample();
//        resourceExample.setOrderByClause("id desc");
        //查询结果
        List<Resource> resourceList = this.resourceMapper.selectByExample(resourceExample);
        PageInfo<Resource> pageInfo = new PageInfo<>(resourceList);
        //设置总条数
        pageDto.setTotal(pageInfo.getTotal());
        //封装成DTO传输
        List<ResourceDto> resourceDtoList = new ArrayList<>();
        for (Resource resource : resourceList) {
            ResourceDto resourceDto = new ResourceDto();
            BeanUtils.copyProperties(resource, resourceDto);
            resourceDtoList.add(resourceDto);
        }
        //设置记录数
        pageDto.setList(resourceDtoList);
    }

    @Override
    public void save(ResourceDto resourceDto) {
        //DTO转~
        Resource resource = new Resource();
        BeanUtils.copyProperties(resourceDto, resource);
        //算法算法生成ID
        SnowFlake snowFlake = new SnowFlake(
                CourseConstants.dataCenterId,
                CourseConstants.machineId);
        resource.setId(String.valueOf(snowFlake.nextId()));
        this.resourceMapper.insertSelective(resource);
    }

    @Override
    public void update(ResourceDto resourceDto) {
        Resource resource = new Resource();
        BeanUtils.copyProperties(resourceDto, resource);
        this.resourceMapper.updateByPrimaryKeySelective(resource);
    }

    @Override
    public ResourceDto findById(String id) {
        Resource resource = this.resourceMapper.selectByPrimaryKey(id);
        ResourceDto resourceDto = new ResourceDto();
        BeanUtils.copyProperties(resource, resourceDto);
        return resourceDto;
    }

    @Override
    public void deleteById(String id) {
        this.resourceMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<ResourceDto> findTreeResources() {
        List<Resource> resourceList = this.resourceMapper.selectByExample(null);
        List<ResourceDto> resourceDtoList = new ArrayList<>();
        Map<String, ResourceDto> map = new HashMap<>();
        for (Resource resource : resourceList) {
            ResourceDto resourceDto = new ResourceDto();
            BeanUtils.copyProperties(resource, resourceDto );
            map.put(resourceDto.getId(), resourceDto);
        }
        for (Resource resource : resourceList) {
            String parentId = resource.getParent();
            String id = resource.getId();
            if ("0".equals(parentId)) {
                //根节点
                resourceDtoList.add(map.get(id));
            } else {
                //不是根节点
                map.get(parentId).getChildren().add(map.get(id));
            }
        }
        return resourceDtoList;
    }

    @Override
    public List<String> findResourcesByRoleId(String roleId) {
        RoleResourceExample roleResourceExample = new RoleResourceExample();
        roleResourceExample.createCriteria().andRoleIdEqualTo(roleId);
        List<RoleResource> roleResources = this.roleResourceMapper.selectByExample(roleResourceExample);
        List<String> resourceIds = new ArrayList<>();
        for (RoleResource roleResource : roleResources) {
            resourceIds.add(roleResource.getResourceId());
        }
        return resourceIds;
    }

    @Override
    public List<ResourceDto> findAll() {
        List<Resource> resourceList = this.resourceMapper.selectByExample(null);
        List<ResourceDto> resourceDtoList = new ArrayList<>();
        for (Resource resource : resourceList) {
            ResourceDto resourceDto = new ResourceDto();
            BeanUtils.copyProperties(resource, resourceDto);
            resourceDtoList.add(resourceDto);
        }
        return resourceDtoList;
    }
}
